#coding=utf-8
import os, datetime
os.environ['DJANGO_SETTINGS_MODULE'] = 'fdd.settings'
from fdd.pindao.models import PinDao,Page
from pindao_html import md5, get_pindao_html, process_pindao_html, decode_html

ROOTDIR = '/home/jay/fdd'
for pd in PinDao.objects.all():
  print pd.name
  h = get_pindao_html(pd.url)
  md5_code = md5(h)
  
  #测试有没有更新
  p = pd.latest_page
  if p and p.md5 == md5_code:
      print 'no new, updated from db.'
  else:    
    dh = decode_html(h)  
    p = Page(pindao=pd, content=dh, md5=md5_code)
    p.save()
    pd.page_last_updated_at = datetime.datetime.now()
    pd.save()
  
  h = process_pindao_html(h, pd)
  open('%s/%s'%(ROOTDIR, pd.fn), 'w').write(h)

